package com.heima.wemedia.service.impl;

import com.heima.common.constants.WemediaConstants;
import com.heima.model.comment.pojos.CommentSaveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.service.CommentService;
import com.heima.wemedia.service.GreenScanService;
import com.heima.wemedia.service.WmNewsAutoScanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired
    private GreenScanService greenScanService;
    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;
    /**
     * 审核评论
     * @param dto
     * @return
     */
    @Override
    public ResponseResult checkComment(CommentSaveDto dto) {
        //1、先敏感词过滤
        if (!wmNewsAutoScanService.handleSensiticeScan(dto.getContent(),null)) {
            //有敏感词
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENT_SENSITIVE);
        }
        //2、再文本审核
        Map map = greenScanService.scanText(dto.getContent());
        if (map != null) {
            if ((int)map.get("conclusionType") == WemediaConstants.BAIDU_SCAN_ILLEGAL) {
                //违规
                return ResponseResult.errorResult(AppHttpCodeEnum.COMMENT_ILLEGAL);
            }
            if ((int)map.get("conclusionType") == WemediaConstants.BAIDU_SCAN_SUSPECTED) {
                //疑似违规
                return ResponseResult.errorResult(AppHttpCodeEnum.COMMENT_SUSPECTED);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
